package hu.uniobuda.nik.bsik2f;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TagAdapter extends ArrayAdapter<TagInfo> {

	int layoutResourceId;
	Context context;
	List<TagInfo> data = null;

	public TagAdapter(Context context, int layoutResourceId, List<TagInfo> data) {
		super(context, layoutResourceId, data);
		this.context = context;
		this.data = data;
		this.layoutResourceId = layoutResourceId;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		TagHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new TagHolder();
			holder.imgCover = (ImageView) row.findViewById(R.id.listItemCover);
			holder.txtArtist = (TextView) row.findViewById(R.id.listItemArtist);
			holder.txtTitle = (TextView) row.findViewById(R.id.listItemTitle);

			row.setTag(holder);
		} else {
			holder = (TagHolder) row.getTag();
		}

		TagInfo tag = data.get(position);
		Bitmap bm = tag.getCoverArtThumb(R.dimen.thumbnail_size);
		if (bm != null) {
			holder.imgCover.setImageBitmap(bm);
		} else
			holder.imgCover.setImageDrawable(context.getResources()
					.getDrawable(R.drawable.nocover));
		if (tag.artist != "" && tag.title != "") {
			holder.txtArtist.setText(tag.artist);
			holder.txtTitle.setText(tag.title);
		}
		else if (tag.title ==""){
			holder.txtTitle.setText(new File(tag.path).getName());
		}
		
		holder.filePath = tag.path;

		return row;
	}

}
